package org.lex.dbq.model.service;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.lex.dbq.model.bo.CommonObject;

public abstract class CommonService<T extends CommonObject> extends BaseService<T> {
	protected Map<String, T> nameMap = new ConcurrentHashMap<String, T>();

	public void add(T o) {
		super.add(o);
		nameMap.put(o.getName(), o);
	}

	@Override
	public T remove(T o) {
		if (null != o) {
			nameMap.remove(o.getName());
		}
		return super.remove(o);
	}

	@Override
	public T removeById(String id) {
		T o = super.removeById(id);
		if (null != o) {
			nameMap.remove(o.getName());
		}
		return o;
	}

	public T removeByName(String name) {
		T o = nameMap.remove(name);
		if (null != o) {
			idMap.remove(o.getId());
		}
		return o;
	}

	public T getByName(String name) {
		return nameMap.get(name);
	}

	public Collection<String> getAllNames() {
		return nameMap.keySet();
	}
}
